﻿using LdtDataEngin.ModuleDetail.CaseCauseElementExtr;
using LdtDataEngin.ModuleDetail.CommonModule.XmlCodeCommon;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using Newtonsoft.Json;
using System;
using System.Diagnostics;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 案由要素提取模块
    /// </summary>
    public class MoudleCaseCauseElementExtract : MoudleBase
    {
        private IMoudle _moudle;

        private static string _moudleResult;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="moudle"></param>
        public MoudleCaseCauseElementExtract(IMoudle moudle)
        {
            _moudle = moudle;
        }

        /// <summary>
        /// 案由要素提取模块
        /// </summary>
        /// <param name="dataContext"></param>
        public override void MoudleHandle(DataContext dataContext)
        {
            _moudleResult = string.Empty;
            string moudleName = typeof(MoudleCaseCauseElementExtract).FullName;
            moudleName += "_" + this.GetHashCode();
            Stopwatch swtext = new Stopwatch();
            swtext.Start();
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
            //执行当前模块的相关业务逻辑；
            try
            {
                dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->开始案由要素提取模块*****************************");
                XmlCodeStartModel xmlCodeStartModel = new XmlCodeStartModel() { dataContext = dataContext };
                var configModel = dataContext.DataLayerSys.GetConfigModel(moudleName);
                if (xmlCodeStartModel.Extract(configModel))
                {
                    CaseCauseElementExtractAssemble ccElementExtractAssemble = new CaseCauseElementExtractAssemble(dataContext, moudleName);
                    ccElementExtractAssemble.Extrat();
                }

            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("案由要素提取模块错误：" + ex.ToString(), LogMessageType.Error);
            }
           
            swtext.Stop();
            dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->案由要素提取模块完成耗时:" + swtext.Elapsed.TotalMilliseconds.ToString("0") + "*****************************");
            if (dataContext.DataLayerAns.ElementAnalysisResults != null)
            {
                _moudleResult = JsonConvert.SerializeObject(dataContext.DataLayerAns.ElementAnalysisResults);
            }
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + swtext.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            //执行完毕调用下一个模块；
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if (!string.IsNullOrWhiteSpace(_moudleResult))
            {
                resultModel.ResultType = "1";
                resultModel.ResultData = _moudleResult;
            }
            return resultModel;
        }

    }
}
